import Ability from '@ohos.application.Ability'
import HiLog from '@ohos.hilog'
import AbilityConstant from '@ohos.application.AbilityConstant'
import Want from '@ohos.application.Want'
import WantParam from '@ohos.application.Want'
import appRecovery from '@ohos.application.appRecovery'

class ContentStorage {

}
export default class MainAbility extends Ability {
    contentStorage : ContentStorage
    onCreate(want, launchParam) {
        console.log("[Demo] AppRecovery MainAbility onCreate 0")
        globalThis.abilityWant = want;
        console.log("[Demo] AppRecoveryDemo onCreate"  + JSON.stringify(want))
        this.contentStorage = new ContentStorage();
        this.context.restoreWindowStage(this.contentStorage);
        console.log("[Demo] AppRecovery MainAbility onCreate 1")
        if (want.flags == 4096) {
            console.log("[Demo] AppRecovery Recovery restart")
            AppStorage.SetOrCreate<string>('StartReason', 'Recovery')
        } else {
            console.log("[Demo] AppRecovery Normal restart")
            AppStorage.SetOrCreate<string>('StartReason', 'Normal')
        }

    }

    onDestroy() {
        console.log("[Demo] MainAbility onDestroy")
    }

    onWindowStageCreate(windowStage) {
        // Main window is created, set main page for this ability
        console.log("[Demo] MainAbility onWindowStageCreate")

        windowStage.loadContent("pages/index", (err, data) => {
            if (err.code) {
                console.error('Failed to load the content. Cause:' + JSON.stringify(err));
                return;
            }
            console.info('Succeeded in loading the content. Data: ' + JSON.stringify(data))
        });
    }

    onSaveState(state, wantParams) {
        console.info('AppRecovery onSaveState. state: ' + JSON.stringify(state))
        console.info('AppRecovery wantParams. wantParams0: ' + JSON.stringify(wantParams))
        wantParams["test"] = "bbb"
        console.info('AppRecovery wantParams. wantParams1: ' + JSON.stringify(wantParams))
        return 0
    }

    onRestoreState(state, wantParams) {
        console.info('AppRecovery onRestoreState. state: ' + JSON.stringify(state))
        console.info('AppRecovery wantParams. wantParams: ' + JSON.stringify(wantParams))
    }

    onWindowStageDestroy() {
        // Main window is destroyed, release UI related resources
        console.log("[Demo] MainAbility onWindowStageDestroy")
    }

    onForeground() {
        // Ability has brought to foreground
        console.log("[Demo] MainAbility onForeground")
        //throw new TypeError("throw error in aafwk callback")
    }

    onContinue(wantParam) {
        console.log("[Demo] MainAbility onContinue")
        return AbilityConstant.OnContinueResult.AGREE
    }

    onBackground() {
        // Ability has back to background
        console.log("[Demo] MainAbility onBackground")
    }
};
